package com.gw.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

@Component
public class LogGatewayFilterFactory extends AbstractGatewayFilterFactory<LogGatewayFilterFactory.Config> {
    public static final String CONSOLE_LOG = "conLog";
    public static final String CACHE_LOG = "cacheLog";

    public LogGatewayFilterFactory() {
        super(Config.class);
    }

    public List<String> shortcutFieldOrder() {
        return Arrays.asList("conLog","cacheLog");
    }

    public GatewayFilter apply(Config config) {
        return new GatewayFilter() {
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

                if(config.conLog){
                    System.out.println("开启了控制台的日志");
                    //
                    System.out.println("自己的逻辑代码---------");
                }
                if(config.cacheLog){
                    System.out.println("开启了缓存的日志。");
                    System.out.println("自己的逻辑代码缓存---------");
                }
                return chain.filter(exchange);
            }

            public String toString() {
                return null;
            }
        };
    }

    public static class Config {
       private Boolean conLog;
       private Boolean cacheLog;

        public Config() {
        }

        public Boolean getConLog() {
            return conLog;
        }

        public void setConLog(Boolean conLog) {
            this.conLog = conLog;
        }

        public Boolean getCacheLog() {
            return cacheLog;
        }

        public void setCacheLog(Boolean cacheLog) {
            this.cacheLog = cacheLog;
        }
    }
}
